package com.dawn.module.cms.service.article;

import com.dawn.module.cms.controller.admin.article.vo.ArticlePageReqVO;
import com.dawn.module.cms.controller.admin.article.vo.ArticleSaveReqVO;
import com.dawn.module.cms.controller.app.article.vo.AppArticlePageReqVO;
import com.dawn.module.cms.dal.dataobject.article.CmsArticleDO;
import com.dawn.module.cms.dal.dataobject.category.CategoryDO;
import com.dawn.module.cms.dal.mysql.category.CmsCategoryMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.util.object.BeanUtils;

import com.dawn.module.cms.dal.mysql.article.CmsArticleMapper;

import static com.dawn.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.dawn.module.cms.enums.ErrorCodeConstants.ARTICLE_NOT_EXISTS;


/**
 * 文章 Service 实现类
 *
 * @author yyh
 */
@Service
@Validated
public class CmsArticleServiceImpl implements CmsArticleService {

    @Resource
    private CmsArticleMapper cmsArticleMapper;

    @Resource
    private CmsCategoryMapper cmsCategoryMapper;

    @Override
    public Long createArticle(ArticleSaveReqVO createReqVO) {
        // 插入
        CmsArticleDO article = BeanUtils.toBean(createReqVO, CmsArticleDO.class);
        if(article.getCategoryId()!=null){
            CategoryDO category =  cmsCategoryMapper.selectById(article.getCategoryId());
            if(category!=null){
                article.setCategoryKey(category.getCategoryKey());
            }
        }
        cmsArticleMapper.insert(article);
        // 返回
        return article.getId();
    }

    @Override
    public void updateArticle(ArticleSaveReqVO updateReqVO) {
        // 校验存在
        validateArticleExists(updateReqVO.getId());
        // 更新
        CmsArticleDO updateObj = BeanUtils.toBean(updateReqVO, CmsArticleDO.class);

        if(updateObj.getCategoryId()!=null){
            CategoryDO category =  cmsCategoryMapper.selectById(updateObj.getCategoryId());
            if(category!=null){
                updateObj.setCategoryKey(category.getCategoryKey());
            }
        }
        cmsArticleMapper.updateById(updateObj);
    }

    @Override
    public void deleteArticle(Long id) {
        // 校验存在
        validateArticleExists(id);
        // 删除
        cmsArticleMapper.deleteById(id);
    }

    private void validateArticleExists(Long id) {
        if (cmsArticleMapper.selectById(id) == null) {
            throw exception(ARTICLE_NOT_EXISTS);
        }
    }

    @Override
    public CmsArticleDO getArticle(Long id) {
        return cmsArticleMapper.selectById(id);
    }

    @Override
    public PageResult<CmsArticleDO> getArticlePage(ArticlePageReqVO pageReqVO) {
        return cmsArticleMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<CmsArticleDO> getArticlePage(AppArticlePageReqVO pageReqVO) {
        return cmsArticleMapper.selectPage(pageReqVO);
    }

}